package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.annotation.ControllerEndpoint;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.BagUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Title:
 * Author: fusu
 * DateTime:  2023/3/24
 * Description:
 **/
@Api(tags = "物资模块-物资类别相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;


    @ApiOperation(value = "物资分类列表展示")
    @GetMapping("/categoryTree")
    public ResponseBean suppliesCategoryTree(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                             @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PageVO<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = productCategoryService.suppliesCategoryTree(pageNum, pageSize);
        return ResponseBean.success(productCategoryTreeNodeVOList);
    }


    @ApiOperation(value = "获取分类树")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOs = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(productCategoryTreeNodeVOs);
    }

    @ApiOperation(value = "物资分类列表添加")
    @PostMapping("/add")
    public ResponseBean addProductCategory(@RequestBody ProductCategory productCategory) {
        productCategoryService.addProductCategory(productCategory);
        return ResponseBean.success();
    }

    @ApiOperation(value = "物资分类列表根据id获取")
    @GetMapping("/edit/{pcId}")
    public ResponseBean getProductCategory(@PathVariable("pcId") Long pcId) {
        ProductCategory productCategory = productCategoryService.getProductCategory(pcId);
        return ResponseBean.success(productCategory);
    }

    @ApiOperation(value = "根据主键id更改分类")
    @PutMapping("update/{pcId}")
    public ResponseBean updateByPcId(@PathVariable("pcId") Long pcId, @RequestBody ProductCategory productCategory) {
        productCategory.setId(pcId);
        productCategoryService.updateByPcId(productCategory);
        return ResponseBean.success();
    }

    @ControllerEndpoint(exceptionMessage = "删除失败", operation = "删除物资分类")
    @ApiOperation(value = "根据主键id删除物资分类")
    @DeleteMapping("/delete/{pcId}")
    public ResponseBean deleteByPcId(@PathVariable("pcId") Long pcId) throws BusinessException {
        productCategoryService.deleteByPcId(pcId);
        return ResponseBean.success();
    }
}
